package com.deodar.web.controller.system;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.deodar.common.core.controller.BaseController;
import com.deodar.common.core.domain.AjaxResult;
import com.deodar.framework.web.domain.server.Sys;
import com.deodar.system.domain.*;
import com.deodar.system.service.*;
import com.deodar.system.utils.MLKContext;
import com.deodar.system.utils.XMLGenerator_NB;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

@Controller
@RequestMapping("/nbxx")
public class NbxxController extends BaseController {

    private final static Logger logger = LoggerFactory.getLogger(NbxxController.class);

    @Resource
    private IAnnualReportInfoService annualReportInfoService;

    @Resource
    private ISubscribedInfoService subscribedInfoService;

    @Resource
    private IPaidInfoService paidInfoService;

    @Resource
    private IInvesInfoService invesInfoService;

    @Resource
    private IWebsiteInfoService websiteInfoService;

    @Resource
    private IEquityChangeInfoService equityChangeInfoService;

    @Resource
    private IAssetStatusInfoService assetStatusInfoService;

    @Resource
    private IStatInfoService statInfoService;

    @Resource
    private ILiaisonInfoService liaisonInfoService;

    @GetMapping("/export")
    @ResponseBody
    public AjaxResult export(){
        Long beginTime = System.currentTimeMillis();
        int startNd = 2019;
        int endNd = 2020;
        int count = annualReportInfoService.getCount(startNd, endNd);
        System.out.println("总共有" + count + "条数据等待转换");
        int page = count/100 + 1;
        System.out.println("每一百条数据为一页，总共有" + page + "页");
        for(int i=0; i< 1; i++) {
            startPage(i + 1, 100);

            // startPage(1,3);
            List<AnnualReportInfo> infos = annualReportInfoService.findList(startNd, endNd);
            System.out.println("企业数：" + infos.size());
            for (AnnualReportInfo info : infos) {
                System.out.println(info.getEntname());
                List<SubscribedInfo> subscribedInfos = subscribedInfoService.findByPripid(info.getPripid());
                info.setSubscribeds(subscribedInfos);
                System.out.println("认缴人数：" + subscribedInfos.size());

                List<PaidInfo> paidInfos = paidInfoService.findByPripid(info.getPripid());
                info.setPaids(paidInfos);
                System.out.println("实缴人数：" + paidInfos.size());

                List<InvesInfo> invesInfos = invesInfoService.findByPripid(info.getPripid());
                info.setInves(invesInfos);
                System.out.println("对外投资企业数：" + invesInfos.size());

                List<WebsiteInfo> websiteInfos = websiteInfoService.findByPripid(info.getPripid());
                info.setWebsites(websiteInfos);
                System.out.println("网站数：" + websiteInfos.size());

                List<EquityChangeInfo> equityChangeInfos = equityChangeInfoService.findByPripid(info.getPripid());
                info.setEquityChanges(equityChangeInfos);
                System.out.println("股权转让数：" + equityChangeInfos.size());

                AssetStatusInfo assetStatusInfo = assetStatusInfoService.findByPripid(info.getPripid());
                info.setAssetStatusInfo(assetStatusInfo);
                System.out.println("资产信息：" + assetStatusInfo.getNetinc());

                StatInfo statInfo = statInfoService.findByPripid(info.getPripid());
                info.setStatInfo(statInfo);
                System.out.println("统计信息：" + statInfo.getHoldingsmsg());

                LiaisonInfo liaisonInfo = liaisonInfoService.findByPripid(info.getPripid());
                info.setLiaisonInfo(liaisonInfo);
                System.out.println("联络信息：" + liaisonInfo.getName());
            }

            Long finishTime = System.currentTimeMillis();
            System.out.println("完成，耗时：" + (finishTime - beginTime) / 1000);
            MLKContext context = new MLKContext();
            context.setDwdm("610000");
            context.setDwmc("陕西省统计局");
            String startTime = DateUtil.date(System.currentTimeMillis()).toString("yyyyMMddhhmmss");
            context.setStartTime(startTime);
            String endTime = DateUtil.date(System.currentTimeMillis()).toString("yyyyMMddhhmmss");
            context.setEndTime(endTime);
            context.setXmlSavePath("D:/exportData");
            String sjbbh = context.getSjbbh(i+1);
            XMLGenerator_NB generator = new XMLGenerator_NB(context);
            try {
                generator.generateXML(infos, sjbbh);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return AjaxResult.success();
    }

    @GetMapping("/fastExport")
    @ResponseBody
    public AjaxResult fastExport(){
        Long beginTime = System.currentTimeMillis();
        int startNd = 2019;
        int endNd = 2020;

        List<AnnualReportInfo> annualReportInfos =
                annualReportInfoService.findList(startNd, endNd);
        logger.info("AnnualReportInfo一共查询到【" + annualReportInfos.size() + "】条数据");

        List<SubscribedInfo> subscribedInfos = subscribedInfoService.findList(startNd,
                endNd);
        logger.info("SubscribedInfo一共查询到【" + subscribedInfos.size() + "】条数据");

        List<PaidInfo> paidInfos = paidInfoService.findList(startNd, endNd);
        logger.info("PaidInfo一共查询到【" + paidInfos.size() + "】条数据");

        List<InvesInfo> invesInfos = invesInfoService.findList(startNd, endNd);
        logger.info("InvesInfo一共查询到【" + invesInfos.size() + "】条数据");

        List<WebsiteInfo> websiteInfos = websiteInfoService.findList(startNd, endNd);
        logger.info("WebsiteInfo一共查询到【" + websiteInfos.size() + "】条数据");

        List<EquityChangeInfo> equityChangeInfos =
                equityChangeInfoService.findList(startNd, endNd);
        logger.info("EquityChangeInfo一共查询到【" + equityChangeInfos.size() + "】条数据");

        List<AssetStatusInfo> assetStatusInfos =
                assetStatusInfoService.findList(startNd, endNd);
        logger.info("AssetStatusInfo一共查询到【" + assetStatusInfos.size() + "】条数据");

        List<StatInfo> statInfos = statInfoService.findList(startNd, endNd);
        logger.info("StatInfo一共查询到【" + statInfos.size() + "】条数据");

        List<LiaisonInfo> liaisonInfos = liaisonInfoService.findList(startNd, endNd);
        logger.info("LiaisonInfo一共查询到【" + liaisonInfos.size() + "】条数据");

        List<AnnualReportInfo> exportList = new LinkedList<>();
        int fileNum = 0;
        MLKContext context = new MLKContext();
        context.setDwdm("610000");
        context.setDwmc("陕西省统计局");
        String startTime = DateUtil.date(System.currentTimeMillis()).toString("yyyyMMddhhmmss");
        context.setStartTime(startTime);
        String endTime = DateUtil.date(System.currentTimeMillis()).toString("yyyyMMddhhmmss");
        context.setEndTime(endTime);
        context.setXmlSavePath("D:/exportData");
        for(AnnualReportInfo info : annualReportInfos){
            String pripid = info.getPripid();
            if(StrUtil.isEmpty(pripid)){
                // 如果pripid为空 那个数据有问题 废弃 继续下一条
                continue;
            }
            // 查找这条数据有多少认缴信息
            for (SubscribedInfo subscribedInfo : subscribedInfos){
                if(pripid.equals(subscribedInfo.getPripid())){
                    // 如果pripid相等，则是关联的有效数据
                    info.addSubscribeds(subscribedInfo);
                }
            }

            // 查找这条数据有多少实缴信息
            for (PaidInfo paidInfo : paidInfos){
                if (pripid.equals(paidInfo.getPripid())) {
                    info.addPaids(paidInfo);
                }
            }

            // 查找这条数据有多少投资信息
            for (InvesInfo invesInfo : invesInfos){
                if(pripid.equals(invesInfo.getPripid())){
                    info.addInves(invesInfo);
                }
            }

            // 查找这条数据有多少网店信息
            for (WebsiteInfo websiteInfo : websiteInfos){
                if (pripid.equals(websiteInfo.getPripid())){
                    info.addWebsites(websiteInfo);
                }
            }

            // 查找这条数据有多少股权变更信息
            for (EquityChangeInfo equityChangeInfo : equityChangeInfos){
                if (pripid.equals(equityChangeInfo.getPripid())){
                    info.addEquityChanges(equityChangeInfo);
                }
            }

            // 查找这条数据有多少企业资产状态信息
            for (AssetStatusInfo assetStatusInfo : assetStatusInfos){
                if (pripid.equals(assetStatusInfo.getPripid())){
                    info.setAssetStatusInfo(assetStatusInfo);
                    break;  // 会查到多条相同的冗余信息，提升效率主动退出循环
                }
            }

            // 查找这条数据有多少统计信息
            for (StatInfo statInfo : statInfos){
                if (pripid.equals(statInfo.getPripid())){
                    info.setStatInfo(statInfo);
                    break;
                }
            }

            // 查找这条数据有多少条工商联络员信息
            for (LiaisonInfo liaisonInfo : liaisonInfos){
                if (pripid.equals(liaisonInfo.getPripid())){
                    info.setLiaisonInfo(liaisonInfo);
                    break;
                }
            }

            exportList.add(info);  // 将包装好的这条数据添加到导出列表中
            if (exportList.size() >= 100){
                fileNum ++;
                if (fileNum % 100 == 0){
                    logger.info("已生成" + fileNum + "个文件");
                }

                String sjbbh = context.getSjbbh(fileNum);
                XMLGenerator_NB generator = new XMLGenerator_NB(context);
                try {
                    generator.generateXML(exportList, sjbbh);
                    logger.info("导出第" + fileNum + "个XML成功");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        if (exportList.size() > 0){
            fileNum ++;
            String sjbbh = context.getSjbbh(fileNum);
            XMLGenerator_NB generator = new XMLGenerator_NB(context);
            try {
                generator.generateXML(exportList, sjbbh);
                logger.info("导出第" + fileNum + "个XML成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        logger.info("XML文件生成完毕~");
        Long finishTime = System.currentTimeMillis();
        double second = (finishTime - beginTime)/1000;
        logger.info("一共生成数据 " + annualReportInfos.size() + " 条,耗时 " + second + " 秒." );
        return AjaxResult.success("一共生成数据 " + annualReportInfos.size() + " 条,耗时 " + second + " 秒.");
    }
}
